﻿using IconYuan.Manager.Markets;
using IconYuan.Manager.Users;
using IconYuan.Model.DTO.Users;
using IconYuan.Model.Entity.Markets;
using IconYuan.Model.Entity.Users;
using SqlSugar;

namespace IconYuan.Api.Services
{
    public class DtoHelper
    {
        static object lockObj = new object();
        public static UserInfoDto ToUserInfoDto(UserBase user, UserDetail userDetail, UserGrade grade)
        {
            var info = new UserInfoDto()
            {
                userInfo = user,
                userDetail = userDetail
            };
            if (grade != null)
            {
                info.gradeInfo = new UserGradeDto()
                {
                    grade_id = grade.grade_id,
                    name = grade.name,
                    nick_name = grade.nick_name,
                    start_time = user.create_time,
                    end_time = user.expire_time,
                    upgrade = grade.upgrade,
                };
            }
            return info;
        }



        //缓存促销活动
        static List<Promotion> _promotion_list;        
        public static List<Promotion> GetPromotionList(int store_id)
        {
            if (_promotion_list == null)
            {
                lock (lockObj)
                {
                    var promotionSvc = new PromotionManager();
                    _promotion_list = promotionSvc.GetList(x => x.store_id == store_id && x.status == 1,
                        y => y.weight, OrderByType.Desc);
                }               
            }
            return _promotion_list;
        }

        static List<UserGrade> _grade_list;
        public static List<UserGrade> GetGradeList()
        {
            if (_grade_list == null)
            {
                lock (lockObj)
                {
                    var gradeSvc = new UserGradeManager();
                    _grade_list = gradeSvc.GetAllList(OrderByType.Asc);
                }
            }
            return _grade_list;
        }
    }
}
